﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.OpenProject
{
    public partial class opensituation : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYKFRCCX");//必须写在ispostback外
            strPageUserType.Add("mana");

            if (!Page.IsPostBack)
            {
                LabMS.Model.SysSetting msys = new LabMS.Model.SysSetting();
                ExtendBLL.SysSetting extendsys = new ExtendBLL.SysSetting();
                try
                {
                    msys = extendsys.GetCurrentSetting();
                }
                catch (Exception ex)
                {
                    lbErr.Visible = true;
                    lbErr.Text = ex.Message;
                    return;
                }
                bindddl();
                

                ddlSemester.SelectedValue = msys.CurrentSemester.Value.ToString();
                ddlYear.SelectedValue = msys.CurrentYear.ToString();
                IniPage(ddlYear.SelectedValue, ddlSemester.SelectedValue);
            }
        }

        void bindddl()
        {
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            try
            {
                listCClass = eDictionary.GetItemListByDClass("ruxuenianfen");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
                return;
            }
            
            ddlYear.DataSource = listCClass;
            ddlYear.DataTextField = "Name";
            ddlYear.DataValueField = "Code";
            ddlYear.DataBind();

        }

        void IniPage(string strYear, string strSemester)
        {
            List<LabMS.Model.Lab> listlab = new List<LabMS.Model.Lab>();
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            try
            {
                listlab = blab.GetModelList("");
            }
            catch (Exception ex)
            {
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
                return;
            }
            

            List<LabMS.Model.OpentProject> listopproject = new List<LabMS.Model.OpentProject>();
            LabMS.BLL.OpentProject bopproject = new LabMS.BLL.OpentProject();
            List<LabMS.Model.Teacher> listmteacher = new List<LabMS.Model.Teacher>();
            LabMS.BLL.Teacher bteacher = new LabMS.BLL.Teacher();
            List<LabMS.Model.ProjectAppointment> listprojectapp = new List<LabMS.Model.ProjectAppointment>();
            ExtendBLL.ProjectAppointment bprojectapp = new ExtendBLL.ProjectAppointment();

            string strtotalTable = "<table cellpadding='5' cellspacing='0' border='1' style='width: 100%; border-collapse: collapse;' class='infoTable'>";
            strtotalTable += "<thead>";
            strtotalTable += "<tr class='header'>";
            strtotalTable += "<th scope='col'>";
            strtotalTable += "实验室";
            strtotalTable += "</th>";
            strtotalTable += "<th scope='col'>";
            strtotalTable += "开放项目";
            strtotalTable += "</th>";
            strtotalTable += "</tr>";
            strtotalTable += "</thead>";

            strtotalTable += "<tbody>";

            foreach (LabMS.Model.Lab l in listlab)
            {
                strtotalTable += "<tr>";
                strtotalTable += "<td>";
                strtotalTable += l.Lab_Name;
                strtotalTable += "</td>";
                strtotalTable += "<td>";
                try
                {
                    listopproject = bopproject.GetModelList(" SYear=" + strYear + " and Semester = " + strSemester
                                                                            + "  and Project_Lab='" + l.Lab_Code + "' order by ID");
                }
                catch (Exception ex)
                {
                    lbErr.Visible = true;
                    lbErr.Text = ex.Message;
                    return;
                }
               
                if (listopproject.Count > 0)
                {
                    strtotalTable += "<table cellpadding='0' cellspacing='0' border='1' style='width: 100%; border-collapse: collapse;' class='infoTable'>";
                    strtotalTable += "<thead>";
                    strtotalTable += "<tr class='header'>";
                    strtotalTable += "<td scope='col'>";
                    strtotalTable += "实验项目";
                    strtotalTable += "</td>";
                    strtotalTable += "<td scope='col'>";
                    strtotalTable += "时间(周次)";
                    strtotalTable += "</td>";
                    strtotalTable += "<td scope='col'>";
                    strtotalTable += "指导老师";
                    strtotalTable += "</td>";
                    strtotalTable += "<td scope='col'>";
                    strtotalTable += "学生数";
                    strtotalTable += "</td>";
                    strtotalTable += "</tr>";
                    strtotalTable += "</thead>";
                    strtotalTable += "<tbody>";
                    foreach (LabMS.Model.OpentProject o in listopproject)
                    {
                        strtotalTable += "<tr>";
                        strtotalTable += "<td>";
                        strtotalTable += o.OP_ProjectName;
                        strtotalTable += "</td>";
                        strtotalTable += "<td>";
                        strtotalTable += o.Project_StartWeek.Value.ToString() + "--" + o.Project_EndWeek.Value.ToString();
                        strtotalTable += "</td>";
                        strtotalTable += "<td>";
                        try
                        {
                            listmteacher = bteacher.GetModelList(" Teacher_Code= '" + o.OP_Teacher + "'");
                        }
                        catch (Exception ex)
                        {
                            lbErr.Visible = true;
                            lbErr.Text = ex.Message;
                            return;
                        }
                        
                        if (listmteacher.Count > 0)
                        {
                            o.OP_Teacher = listmteacher[0].Teacher_Name;
                        }
                        strtotalTable += o.OP_Teacher;
                        strtotalTable += "</td>";
                        strtotalTable += "<td>";
                        try
                        {
                            listprojectapp = bprojectapp.GetModelList(" OP_ProjectCode ='" + o.OP_ProjectCode + "'");
                        }
                        catch (Exception ex)
                        {
                            lbErr.Visible = true;
                            lbErr.Text = ex.Message;
                            return;
                        }
                        
                        o.OP_OpenType = listprojectapp.Count.ToString();
                        strtotalTable += o.OP_OpenType;
                        strtotalTable += "</td>";
                        strtotalTable += "</tr>";
                    }
                    strtotalTable += "</tbody>";
                    strtotalTable += "</table>";
                }
                else
                {
                    strtotalTable += "无";
                }
                strtotalTable += "</td>";
                strtotalTable += "</tr>";
            }

            strtotalTable += "</tbody>";

            strtotalTable += "</table>";
            lbSituation.Text = strtotalTable;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IniPage(ddlYear.SelectedValue, ddlSemester.SelectedValue);
        }
    }
}
